import { RootAction } from '@/types/store'
import { getHistory } from '@/utils/history'

type StateType = {
  suggestion: string[]
  history: string[]
}

const initValue = {
  suggestion: [], // 搜索建议
  history: getHistory(), // 搜索历史
}

export default function searchReducer(
  state: StateType = initValue,
  action: RootAction
) {
  if (action.type === 'search/set_suggestion') {
    return {
      ...state,
      suggestion: action.payload,
    }
  }

  if (action.type === 'search/set_history') {
    return {
      ...state,
      history: action.payload,
    }
  }

  return state
}
